const app = getApp()
import activeApi from '../../api/activeApi'
import {getFishNames} from '../../util/fish'
Page({

  /**
   * 页面的初始数据
   */
  data: {
   active: null,
   imgList: [],
   fishNames: '',
   isJoin:false,
   joinList: [],
    shareImg: '',
    isShow: false,
    markers: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var activeId = ''
    if (options.scene){
      var scene = decodeURIComponent(options.scene);
      activeId = scene
    }else{
      activeId = options.activeId
    }
   
    if(activeId){
      this.getActive(activeId)
    }else{
    
    }
   

  
  },

  getActive(activeId){
    wx.showLoading({
      mask:true
    })
    activeApi.getActive(activeId).then(data => {
      wx.hideLoading( )
      var imgList = data[0].imgList;
      if(typeof imgList == 'string'){
        imgList = imgList.split(',')
      }
      
      this.setData({
        active : data[0],
        imgList: imgList,
        fishNames: getFishNames(data[0].category),
        joinList:data[0].joinList
      })
      
      this.setData({
        markers: [{
          id: data[0]._id,
          latitude: this.data.active.location.coordinates[1],
          longitude: this.data.active.location.coordinates[0],
          name: data[0].address,
          iconPath: data[0].isPlace ? '/images/track-dot.png' : '/images/location.png',
          width: 23,
          height: 31,
          callout: {
            content: data[0].isPlace ? '钓鱼场' : '钓点',
            display: "ALWAYS",
            padding: 5,
            bgColor: '#6495ed',
            color: '#fff',
            fontSize: 20
          }
        }]
      })


      //创建分享图
      app.createShareImg(this.data.active).then(path => {
        this.setData({
          shareImg: path
        });
      })
   
    })
  },

  onMapTap(){

  },
  previewImage(e){
    var url = e.currentTarget.dataset.url
    wx.previewImage({
      urls: this.data.imgList,
      current: url
    })
  },
  openLocation(){
    
    wx.openLocation({
      latitude: this.data.active.location.coordinates[1],
      longitude: this.data.active.location.coordinates[0],
      address: this.data.active.address,
      name: this.data.active.name
    })
  },

  join(){
    if(!app.globalData.userInfo){
      wx.navigateTo({
        url: '/pages/login/login'
      })
      return
    }
   wx.showLoading({
     mask:true
   })
    activeApi.joinActive({
      openid : app.globalData.openid,
      avatarUrl : app.globalData.userInfo.avatarUrl,
      nickname : app.globalData.userInfo.nickname,
      activeId: this.data.active._id
    }).then(res => {
      wx.hideLoading()
      wx.showToast({
        title: '已报名参加',
      })
      this.data.joinList.unshift({
        avatarUrl : app.globalData.userInfo.avatarUrl,
        nickname : app.globalData.userInfo.nickname
      })
      this.setData({
        isJoin:true,
        joinList: this.data.joinList
      })
    })
  },
  getWXACode(){
    app.getWXACode({
      path:'pages/activeDetail/activeDetail',
      scene:this.data.active._id
    }).then(res => {
      
    })
  },
  toShare(){
    wx.setStorageSync("active", this.data.active)
    wx.navigateTo({
      url: '/pages/share/share',
    })
  },
  aginAdd(){
   
    wx.setStorageSync('active',this.data.active)
    wx.navigateTo({
      url: '/pages/addActive/addActive',
    })
  },
  copyContent(){
    wx.setClipboardData({
      data: this.data.active.content,
      success (res) {
        
      }
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    this.selectComponent('#commentList').nextPage()
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
   
      var obj = {
        title: this.data.active.name,
        path: "/pages/activeDetail/activeDetail?activeId="+this.data.active._id
      }
      if(this.data.imgList.length > 0){
        obj.imageUrl = this.data.imgList[0]
      }
  
    
    return obj
  },

  onShareTimeline: function () {
    
       var obj = {
         title: this.data.active.content,
         query: "activeId="+this.data.active._id
       }
       if(this.data.imgList.length > 0){
         obj.imageUrl = this.data.imgList[0]
       }

     
     return obj
   }
})